import { workspace, Memento } from 'vscode';
import { Snippet } from '../types/snippet';
export class SnippetManager {
  constructor(private globalState: Memento) {}

  // 存储片段
  async saveSnippet(snippet: any) {
    const snippets = this.globalState.get('snippets') as any[] || [];
    await this.globalState.update('snippets', [...snippets, snippet]);
  }

// SnippetManager.ts
getSnippets(): any[] {
  // 添加类型校验
  const snippets = this.globalState.get<any[]>('snippets');
  return Array.isArray(snippets) ? snippets : []; // 确保始终返回数组
}
 async reorderSnippets(draggedItems: Snippet[], targetId?: string) {
    const allSnippets = this.getSnippets();
    
    // 移除被拖拽的项
    const filtered = allSnippets.filter(snippet => 
      !draggedItems.find(d => d.id === snippet.id)
    );

    // 查找目标位置
    const targetIndex = targetId ? 
      filtered.findIndex(s => s.id === targetId) : 
      filtered.length;

    // 插入拖拽项
    filtered.splice(targetIndex, 0, ...draggedItems);
    
    // 更新存储
    await this.globalState.update('snippets', filtered);
  }
}
